from mcp.server.fastmcp import FastMCP
import logging

# 配置日志记录
logging.basicConfig(level=logging.INFO)

# 创建MCP服务实例
mcp = FastMCP("Calculator Service")

@mcp.tool()
def say_hello(name: str) -> str:
    """生成个性化的问候消息"""
    logging.info(f"say_hello called with name: {name}")
    return f"你好, {name}! 欢迎使用计算服务!"

@mcp.tool()
def add(a: float, b: float) -> float:
    """两数相加"""
    logging.info(f"add called with a: {a}, b: {b}")
    return a + b

@mcp.tool()
def subtract(a: float, b: float) -> float:
    """两数相减"""
    logging.info(f"subtract called with a: {a}, b: {b}")
    return a - b

@mcp.tool()
def multiply(a: float, b: float) -> float:
    """两数相乘"""
    logging.info(f"multiply called with a: {a}, b: {b}")
    return a * b

@mcp.tool()
def divide(a: float, b: float) -> float:
    """两数相除"""
    logging.info(f"divide called with a: {a}, b: {b}")
    if b == 0:
        raise ValueError("除数不能为零")
    return a / b

@mcp.tool()
def power(a: float, b: float) -> float:
    """计算a的b次方"""
    logging.info(f"power called with a: {a}, b: {b}")
    return a ** b

@mcp.tool()
def square_root(a: float) -> float:
    """计算a的平方根"""
    logging.info(f"square_root called with a: {a}")
    if a < 0:
        raise ValueError("不能计算负数的平方根")
    return a ** 0.5

if __name__ == "__main__":
    mcp.run(transport="stdio")